package org.xfy.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;
import org.xfy.sys.domain.DictionaryType;
import org.xfy.sys.query.DictionaryTypeQuery;
import org.xfy.sys.service.IDictionaryTypeService;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
@Api(value = "数据字典类型的API", description = "数据字典类型相关的CRUD功能")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    @PostMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult save(@RequestBody DictionaryType dictionaryType) {
        if (dictionaryType.getId() == null) {//新增
            dictionaryTypeService.add(dictionaryType);
        } else {
            dictionaryTypeService.update(dictionaryType);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public AjaxResult del(@PathVariable("id") Long id) {
        dictionaryTypeService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public AjaxResult PatchDel(@RequestBody Long[] ids) {
        dictionaryTypeService.patchDel(ids);
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult findOne(@PathVariable("id") Long id) {
        DictionaryType dictionaryType = dictionaryTypeService.findOne(id);
        return AjaxResult.me().setResultObj(dictionaryType);
    }

    @GetMapping()
    @ApiOperation(value = "查询所有")
    public AjaxResult findAll() {
        List<DictionaryType> dictionaryTypes = dictionaryTypeService.findAll();
        return AjaxResult.me().setResultObj(dictionaryTypes);
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DictionaryTypeQuery query) {
        PageList<DictionaryType> pageList = dictionaryTypeService.queryPage(query);
        return AjaxResult.me().setResultObj(pageList);
    }

}
